package util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import common.ServerResponse;

public class myUtil {
	
	//设置头信息
	public static void setHead(HttpServletRequest request, HttpServletResponse response) {
		//转发时候防止中文乱码
	    response.setHeader("content-type", "text/html;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    String originHeader=request.getHeader("Origin");
		/* 允许跨域的主机地址 */ response.setHeader("Access-Control-Allow-Origin", originHeader);
		/* 允许跨域的请求方法GET, POST, HEAD 等 */ response.setHeader("Access-Control-Allow-Methods", "*"); 
		/* 重新预检验跨域的缓存时间 (s) */ response.setHeader("Access-Control-Max-Age", "3600");
		/* 允许跨域的请求头 */ response.setHeader("Access-Control-Allow-Headers", "*"); 
		/* 是否携带cookie */ response.setHeader("Access-Control-Allow-Credentials", "true");
	}
	
	//转成json
	public static void getjson(ServerResponse se,HttpServletResponse response) {
		Gson gson=new Gson();
		String json=gson.toJson(se);
		PrintWriter pw;
		try {
			pw=response.getWriter();
			pw.write(json);
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

}
